import {login,getUserInfo,logout} from '../../api/login'
import Cookies from 'js-cookie'
import {Message} from 'element-ui'
import {Base64} from 'js-base64'
import md5 from 'js-md5'
const user = {
    state:{
        email:'',
        token:'',
        roles:[],
        alias:'',
        headImage:'',
        isLogin:false
    },
    mutations:{
        'SET_TOKEN'(state, token){
            state.token = token
        },
        'SET_EMAIL'(state, email){
            state.email = email
        },
        'SET_ROLES'(state, roles){
            state.roles = roles
        },
        'SET_ISLOGIN'(state, isLogin){
            state.isLogin = isLogin
        },
        'SET_HEADIMAGE'(state, image){
            state.headImage = image
        },
        'SET_ALIAS'(state, alias){
            state.alias = alias
        }
    },
    actions:{
        //获取用户token
        LOGIN(context,userInfo){
            let url = 'http://localhost:8090/auth/getToken'
            return new Promise((resolve, reject) => {
                let data = {
                    username:userInfo.username,
                    password:md5(userInfo.password)
                }
                login(url,data).then(res => {
                    const rs = res.data
                    console.log(rs)
                    context.commit('SET_TOKEN', rs.token)
                    Cookies.set('x-token', rs.token)
                    resolve()
                })
            }).catch(err => {
                console.log(err)
            })
        },
        //获取用户信息
        GETUSERINFO(context,token){
            let url = 'http://localhost:8090/auth/userInfo'
            return new Promise((resovle,reject)=>{
                if(Cookies.get('x-token')){
                    getUserInfo(url,token).then(res => {
                        let rs = res.data
                        context.commit('SET_ISLOGIN', true)
                        context.commit('SET_EMAIL', rs.email)
                        context.commit('SET_ROLES', rs.roles)
                        context.commit('SET_ALIAS', rs.alias)
                        resovle()
                    }).catch(error =>{
                        Message({
                            message: '服务器相应失败，请稍后再试' + error ,
                            type: 'error',
                            duration: 2000
                        })
                    })
                }
            })
        },
        //后端登出
        LOGOUTSERVCE(context){
            return new Promise((resvole,reject)=>{
                let url = 'http://localhost:8090/auth/logout'
                logout(url,email).then(res => {
                    if(res.data.code === 40000){
                        context.commit('SET_ISLOGIN', false)
                        context.commit('SET_EMAIL', '')
                        context.commit('SET_ROLES', '')
                        context.commit('SET_ALIAS', '')
                        Cookies.remove('x-token')
                        Cookies.remove('nextLoginInfo')
                    }else{
                        Message({
                            message: '退出失败',
                            type: 'error',
                            duration: 2000
                        })
                    }
                }).catch(error=>{
                    Message({
                        message: '服务器相应失败，请稍后再试' + error ,
                        type: 'error',
                        duration: 2000
                    })
                })
            })
        }
    }
}

export default user